package cn.jiadakeji.parent.service.impl;


import cn.jiadakeji.parent.common.constant.RedisPrefixConstants;
import cn.jiadakeji.parent.domain.TermTimeCourse;
import cn.jiadakeji.parent.dto.TermTimeCourseDTO;
import cn.jiadakeji.parent.mapper.CustomerMapper;
import cn.jiadakeji.parent.mapper.TermTimeCourseMapper;
import cn.jiadakeji.parent.service.CustomerTeacherService;
import cn.jiadakeji.parent.service.TermTimeCourseService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author heyanfeng
 * @create 2022-03-11 15:46
 */
@Service
public class TermTimeCourseServiceImpl extends ServiceImpl<TermTimeCourseMapper, TermTimeCourse> implements TermTimeCourseService {

    @Autowired
    private CustomerTeacherService teacherService;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 获取课堂列表信息
     */
    @Override
    public List<TermTimeCourseDTO> getTimeCourse(String termCourseId) {
        String data = redisTemplate.opsForValue().get(RedisPrefixConstants.CLASS_LIST_PREFIX + termCourseId);
        if (data != null) {
            return JSON.parseObject(data, new TypeReference<List<TermTimeCourseDTO>>() {
            });
        } else {
            List<TermTimeCourse> timeCourseList = baseMapper.selectList(new QueryWrapper<TermTimeCourse>()
                    .eq("term_course_id", termCourseId));
            // 转换类型
            List<TermTimeCourseDTO> collect = timeCourseList.stream().map(course -> {
                TermTimeCourseDTO termTimeCourseDTO = new TermTimeCourseDTO();
                BeanUtils.copyProperties(course, termTimeCourseDTO);
                // 设置教师名字
                String teacherName = teacherService.getTeacherName(course.getTeacherId());
                termTimeCourseDTO.setTeacherName(teacherName);
                return termTimeCourseDTO;
            }).collect(Collectors.toList());
            String jsonString = JSON.toJSONString(collect);
            redisTemplate.opsForValue().set(RedisPrefixConstants.CLASS_LIST_PREFIX + termCourseId, jsonString, 7, TimeUnit.DAYS);
            return collect;
        }
    }

    /**
     * 获取课节信息
     *
     * @param timeId
     * @return
     */
    @Override
    public TermTimeCourseDTO getTimeCourseById(String timeId) {
        TermTimeCourse termTimeCourse = baseMapper.selectById(timeId);
        // 类型转换
        if (termTimeCourse != null) {
            TermTimeCourseDTO termTimeCourseDTO = new TermTimeCourseDTO();
            BeanUtils.copyProperties(termTimeCourse, termTimeCourseDTO);
            // 设置教师名字
            String teacherName = teacherService.getTeacherName(termTimeCourse.getTeacherId());
            termTimeCourseDTO.setTeacherName(teacherName);
            return termTimeCourseDTO;
        }
        return null;
    }

    @Override
    public TermTimeCourse getNextCourseTime(LocalDateTime now, String courseId) {

        return baseMapper.getNextCourseTime(now, courseId);
    }

    @Override
    public TermTimeCourse getPreCourseTime(String courseId) {
        // 获取上一周的结束时间,即上一周的周日
        Calendar lastWeekend =Calendar.getInstance();
        lastWeekend.add(Calendar.DAY_OF_MONTH,-7);
        lastWeekend.setFirstDayOfWeek(Calendar.MONDAY);
        lastWeekend.set(Calendar.DAY_OF_WEEK, lastWeekend.getFirstDayOfWeek()+ 6);
        // 上一周的今天
        Calendar lastDay =Calendar.getInstance();
        lastDay.add(Calendar.DAY_OF_MONTH,-7);
        // 查找在此时间段的最近的一次课程
        return baseMapper.getPreCourseTime(lastWeekend.getTime(), lastDay.getTime(),courseId);
    }
}
